import { getNewValue } from ".";

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const {sheet, sheetName, row, col, table, store, old } = options;
        context.suspendPaint();
        try {
            const bindingPath = table.bindingPath();
            if (isUndo) {
                store.dataSourceInstance.setValue(bindingPath, old.tableValue);
            } else {
                const tableValue = store.dataSourceInstance.getValue(bindingPath);
                options.old = { tableValue: [...tableValue] };
                const { spanKeys } = table;
                const { row: startRow, col: startCol, rowCount } = table.dataRange();
                const rowIndex = row - startRow;
                const colIndex = col - startCol;
                const newValue = getNewValue(tableValue, rowIndex, spanKeys.filter(d => d.index <= colIndex),1);
                store.dataSourceInstance.setValue(bindingPath, newValue);          
            }
            table.bindingPath(bindingPath);
            store.calcAll(true, sheetName);
            if(!isUndo) {
                const { row: startRow, col: startCol, rowCount } = table.dataRange();
                for(let r = rowCount + startRow ; r > row ; r--) {
                    console.log(r,r-1, sheet.getRowHeight(r-1))
                    sheet.setRowHeight(r, sheet.getRowHeight(r-1));
                }
            }
        } catch (e) {
            if(!!(window.console&&window.console.log)) {
                window.console.log(e);
            }
        }
        context.resumePaint();
    }
};